﻿// ....................................................................................................................
//	Author: Copyright (C) 2012 Ing. Marek Hubal
//	Email: hubal@acasoftware.org
// ....................................................................................................................

using System;

namespace core
{
    /// <summary>Trieda ISplitFileDataStruct</summary>
    public class ISplitFileDataStruct
    {
        public string FileNameToSplit; // Subor na rozdelenie
        public string DestinationPath; // Cesta na ulozenie
        public long SplitSize; // Velkost jednej casti
        public bool CreateCRC; // Priznak vytvarania CRC32
        public long SourceSize; // Velkost zdroja

        public const long PREDEFINED_SIZE_1_44MB = 1457664;
        public const long PREDEFINED_SIZE_100MB = 100431872;
        public const long PREDEFINED_SIZE_250MB = 250331136;
        public const long PREDEFINED_SIZE_650MB = 681574400;
        public const long PREDEFINED_SIZE_700MB = 734003200;
        public const long PREDEFINED_SIZE_4482MB = 4699717632;

        /// <summary>Konstruktor</summary>
        public ISplitFileDataStruct()
        {
            FileNameToSplit = "";
            DestinationPath = "";
            SplitSize = PREDEFINED_SIZE_1_44MB;
            CreateCRC = false;
            SourceSize = 0;
        }
        /// <summary>Skopirovanie obsahu objektu</summary>
        /// <param name="pValue">objekt na skopirovanie</param>
        public void Assign(ISplitFileDataStruct pValue)
        {
            FileNameToSplit = pValue.FileNameToSplit;
            DestinationPath = pValue.DestinationPath;
            SplitSize = pValue.SplitSize;
            CreateCRC = pValue.CreateCRC;
            SourceSize = pValue.SourceSize;
        }
        /// <summary>Ziskanie preddefinovaneho ID</summary>
        /// <param name="pValue">hodnota</param>
        /// <returns>ID</returns>
        public int GetPredefinedID(long pValue)
        {
            if (pValue == PREDEFINED_SIZE_1_44MB) return 0;
            if (pValue == PREDEFINED_SIZE_100MB) return 1;
            if (pValue == PREDEFINED_SIZE_250MB) return 2;
            if (pValue == PREDEFINED_SIZE_650MB) return 3;
            if (pValue == PREDEFINED_SIZE_700MB) return 4;
            if (pValue == PREDEFINED_SIZE_4482MB) return 5;
            return 6;
        }

        /// <summary>Editacia obsahu pomocou formulara</summary>
        /// <returns>true / false</returns>
        public bool EditWithForm()
        {
            using (IFileExplorerForm_SplitFile F = new IFileExplorerForm_SplitFile())
            {
                F.Value.Assign(this); // Skopirujem obsah
                bool B = F.ShowDialog() == System.Windows.Forms.DialogResult.OK; // Zobrazim okno
                if (B) Assign(F.Value); // Skopirujem vysledok
                return B;
            }
        }
    }

    /// <summary>Trieda IFileExplorerForm_SplitFile</summary>
    public class IFileExplorerForm_SplitFile : IForm
    {
        public ISplitFileDataStruct Value; // Objekt pre editaciu

        protected IGroupBox FGrp; // Controls
        protected IButton FBtn_OK;
        protected IButton FBtn_Cancel;

        protected IGroupBox FGrp1, FGrp2;

        protected ILabel FLabel1;
        protected ITextBox FText_SourceFile;
        protected ILabel FLabel2;
        protected ITextBox FText_DestinationFolder;
        protected ILabel FLabel3;
        protected IComboBox FCombo_Sizes;
        protected IComboBox FCombo_SizeTypes;
        protected ITextBox FText_CustomSize;
        protected ICheckBox FCheck_CreateCRC;

        /// <summary>Konstruktor</summary>
        public IFileExplorerForm_SplitFile()
        {
            Value = new ISplitFileDataStruct(); // Inicializacia
        }

        /// <summary>Metoda: OnCreateForm</summary>
        /// <returns>true / false</returns>
        protected override bool OnCreateForm()
        {
            if (!base.OnCreateForm()) return false; // Volam predchodcu

            // Inicializacia
            MinimizeBox = false; MaximizeBox = false; FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; ShowIcon = true; ShowInTaskbar = false; StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

            SetBounds(0, 0, 488, 314); System.Drawing.Size CS = ClientSize;
            Text = lang.FORM_EDIT_SPLIT_FILE_TEXT_1;

            // Vytvorim groupbox
            FGrp = new IGroupBox(); IControl.PlaceControl_Inside(FGrp, this, 10, 10, 10, 50);
            System.Drawing.Size CS1 = FGrp.ClientSize;

            // Vytvorim tlacidla
            FBtn_Cancel = new IButton(lang.FORM_EDIT_SPLIT_FILE_TEXT_3); IControl.PlaceControl_Outside_TopRight_RightBottom(FBtn_Cancel, FGrp, 0, 10); FBtn_Cancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            FBtn_OK = new IButton(lang.FORM_EDIT_SPLIT_FILE_TEXT_2, Event_BtnOKClick); IControl.PlaceControl_Outside_TopRight_LeftTop(FBtn_OK, FBtn_Cancel, 0, 10);

            FLabel1 = new ILabel(lang.FORM_EDIT_SPLIT_FILE_TEXT_4);
            FLabel2 = new ILabel(lang.FORM_EDIT_SPLIT_FILE_TEXT_5);
            FText_SourceFile = new ITextBox(); FText_SourceFile.ReadOnly = true;
            FText_DestinationFolder = new ITextBox(); FText_DestinationFolder.ReadOnly = true;
            FLabel3 = new ILabel(lang.FORM_EDIT_SPLIT_FILE_TEXT_8);
            FCombo_Sizes = new IComboBox(); FCombo_Sizes.AddMultiItems(lang.FORM_EDIT_SPLIT_FILE_TEXT_6, '\b', new int[] { 0, 1, 2, 3, 4, 5, 6 });
            FCombo_SizeTypes = new IComboBox(); FCombo_SizeTypes.AddMultiItems(lang.FORM_EDIT_SPLIT_FILE_TEXT_7, '\b', new int[] { 0, 1, 2, 3 });
            FText_CustomSize = new ITextBox();
            FCheck_CreateCRC = new ICheckBox(lang.FORM_EDIT_SPLIT_FILE_TEXT_9);

            FGrp1 = new IGroupBox(); IControl.PlaceControl_Inside_LeftTop_LeftTop(FGrp1, FGrp, 10, 10, CS1.Width - 20, 120);
            System.Drawing.Size CS2 = FGrp1.ClientSize;
            FGrp1.Controls.AddRange(new System.Windows.Forms.Control[] { FLabel1, FText_SourceFile, FLabel2, FText_DestinationFolder });

            IControl.PlaceControl_Inside_LeftTop_LeftTop(FLabel1, FGrp, 10, 20);
            IControl.PlaceControl_Outside_LeftTop_LeftBottom(FText_SourceFile, FLabel1, 0, 8, CS2.Width - 20);
            IControl.PlaceControl_Outside_LeftTop_LeftBottom(FLabel2, FText_SourceFile, 0, 8);
            IControl.PlaceControl_Outside_LeftTop_LeftBottom(FText_DestinationFolder, FLabel2, 0, 8, CS2.Width - 20);

            FGrp2 = new IGroupBox(); IControl.PlaceControl_Outside_LeftTop_LeftBottom(FGrp2, FGrp1, 0, 6, CS1.Width - 20, 50);
            FGrp2.Controls.AddRange(new System.Windows.Forms.Control[] { FLabel3, FCombo_Sizes, FText_CustomSize, FCombo_SizeTypes });

            IControl.PlaceControl_Inside_LeftTop_LeftTop(FLabel3, FGrp2, 10, 20);
            IControl.PlaceControl_Outside_LeftTop_TopRight(FCombo_Sizes, FLabel3, -2, 10, 100);
            IControl.PlaceControl_Outside_LeftTop_TopRight(FText_CustomSize, FCombo_Sizes, 1, 20, 100);
            IControl.PlaceControl_Outside_LeftTop_TopRight(FCombo_SizeTypes, FText_CustomSize, -1, 8, 50);

            FGrp.Controls.AddRange(new System.Windows.Forms.Control[] { FGrp1, FGrp2, FCheck_CreateCRC });
            IControl.PlaceControl_Outside_LeftTop_LeftBottom(FCheck_CreateCRC, FGrp2, 10, 10);

            Controls.AddRange(new System.Windows.Forms.Control[] { FGrp, FBtn_OK, FBtn_Cancel }); // Zaradim do controls

            AcceptButton = FBtn_OK; CancelButton = FBtn_Cancel; FBtn_OK.Focus();

            FCombo_Sizes.SelectedIndexChanged += Event_SelectedIndexChanged;

            FillForm(Value); // Vyplnim zaciatocnou hodnotou
            return true; // Vsetko OK
        }

        /// <summary>Vyplnenie formulara</summary>
        /// <param name="pValue">objekt s datami</param>
        protected void FillForm(ISplitFileDataStruct pValue)
        {
            FText_SourceFile.Text = pValue.FileNameToSplit;
            FText_DestinationFolder.Text = pValue.DestinationPath;
            FCombo_Sizes.SelectedIntID = pValue.GetPredefinedID(pValue.SplitSize);
            FText_CustomSize.Text = pValue.SplitSize.ToString();
            FCombo_SizeTypes.SelectedIntID = 0;
            FCheck_CreateCRC.Checked = pValue.CreateCRC;
            UpdateForm();
        }
        /// <summary>Nacitanie obsahu formulara</summary>
        /// <param name="Result">vysledok</param>
        protected void ReadForm(ISplitFileDataStruct Result)
        {
            switch (FCombo_Sizes.SelectedIntID)
            {
                case 0: Result.SplitSize = ISplitFileDataStruct.PREDEFINED_SIZE_1_44MB; break;
                case 1: Result.SplitSize = ISplitFileDataStruct.PREDEFINED_SIZE_100MB; break;
                case 2: Result.SplitSize = ISplitFileDataStruct.PREDEFINED_SIZE_250MB; break;
                case 3: Result.SplitSize = ISplitFileDataStruct.PREDEFINED_SIZE_650MB; break;
                case 4: Result.SplitSize = ISplitFileDataStruct.PREDEFINED_SIZE_700MB; break;
                case 5: Result.SplitSize = ISplitFileDataStruct.PREDEFINED_SIZE_4482MB; break;
                default:
                    {
                        long L;
                        switch (FCombo_SizeTypes.SelectedIntID) // Urcim nasobok
                        {
                            case 1: L = 1024; break;
                            case 2: L = 1024*1024; break;
                            case 3: L = 1024*1024*1024; break;
                            default: L = 1; break;
                        }
                        Result.SplitSize = ICore.ToLong(FText_CustomSize.Text) * L; // Vypocitam vyslednu sumu
                    } break;
            }
            Result.CreateCRC = FCheck_CreateCRC.Checked;
        }
        /// <summary>Kontrola validity udajov</summary>
        /// <returns>true / false</returns>
        protected bool CheckForm()
        {
            switch (FCombo_Sizes.SelectedIntID)
            {
                case 6:
                    {
                        if (FText_CustomSize.IsValidLong(1)) return true;
                        IApp.ShowError(lang.FORM_EDIT_SPLIT_FILE_TEXT_10);
                        FText_CustomSize.Focus();
                        return false;
                    }
                default: return true;
            }
        }
        /// <summary>Aktualizacia stavu okna</summary>
        protected void UpdateForm()
        {
            FText_CustomSize.Enabled = FCombo_Sizes.SelectedIntID == 6;
            FCombo_SizeTypes.Enabled = FText_CustomSize.Enabled;
        }

        /// <summary>Event: SelectedIndexChanged</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_SelectedIndexChanged(object pSender, EventArgs e)
        {
            UpdateForm();
            
            FCombo_SizeTypes.SelectedIntID = 0; // Nastavim bajty
            switch (FCombo_Sizes.SelectedIntID)
            {
                case 0: FText_CustomSize.Text = ISplitFileDataStruct.PREDEFINED_SIZE_1_44MB.ToString(); break;
                case 1: FText_CustomSize.Text = ISplitFileDataStruct.PREDEFINED_SIZE_100MB.ToString(); break;
                case 2: FText_CustomSize.Text = ISplitFileDataStruct.PREDEFINED_SIZE_250MB.ToString(); break;
                case 3: FText_CustomSize.Text = ISplitFileDataStruct.PREDEFINED_SIZE_650MB.ToString(); break;
                case 4: FText_CustomSize.Text = ISplitFileDataStruct.PREDEFINED_SIZE_700MB.ToString(); break;
                case 5: FText_CustomSize.Text = ISplitFileDataStruct.PREDEFINED_SIZE_4482MB.ToString(); break;
            }
        }
        /// <summary>Event: Click pre tlacidlo OK</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_BtnOKClick(object pSender, EventArgs e)
        {
            if (!CheckForm()) return; // Kontrola validity

            ReadForm(Value); // Nacitam data
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
